package com.ht.web.emp;

import com.ht.bean.aduit.Aduitlog;
import com.ht.bean.aduit.Aduitmodel;
import com.ht.bean.emp.Education;
import com.ht.bean.emp.Empinfo;
import com.ht.bean.emp.Familyinfo;
import com.ht.bean.emp.Job;
import com.ht.bean.file.Annex;
import com.ht.bean.file.Datadoc;
import com.ht.bean.json.JsonData;
import com.ht.service.aduit.AduitlogService;
import com.ht.service.aduit.AduitmodelService;
import com.ht.service.emp.EducationService;
import com.ht.service.emp.EmpinfoService;
import com.ht.service.emp.FamilyinfoService;
import com.ht.service.emp.JobService;
import com.ht.service.file.AnnexService;
import com.ht.util.Contants;
import com.ht.util.Pager;
import com.ht.util.Utils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

/**
 * 半口香甜吴一玄
 */
@Controller
@RequestMapping("option")
public class empoptionController {
    @Resource
    private EmpinfoService empinfoService;
    @Resource
    private AnnexService annexService;
    @Resource
    private JobService jobService;
    @Resource
    private EducationService educationService;
    @Resource
    private FamilyinfoService familyinfoService;
    @Resource
    private AduitlogService aduitlogservice;
    @Resource
    private AduitmodelService aduitmodelService;
    @Resource
    private JsonData jsonData;

    Pager pager = new Pager();

    /**
     *
     * 工作经历
     *
     * */
    @RequestMapping("towork")
    public String word(Integer id,HttpSession session){
        session.setAttribute("empid",id);
        return "empoption/work";
    }
    //数据
    @RequestMapping("work")
    @ResponseBody
    public JsonData work(@Param("limit")int limit , @Param("page")int page, Integer id){
        pager.setPageSize(limit);
        pager.setCurrPage(page);
        jsonData.setCount(jobService.selCount(id));
        List<Job> list = (jobService.sellist(pager,id));
        jsonData.setData(list);
        return jsonData;
    }

    //新增
    @RequestMapping("toworkadd")
    public String toworkadd(){
        return "empoption/workadd";
    }
    //新增
    @RequestMapping("workadd")
    @ResponseBody
    public Boolean workadd(Job job,HttpSession session){
        Integer id= (Integer) session.getAttribute("empid");
        job.setEmpid(id);
        int bool = jobService.insertSelective(job);
        if (bool < 0){
            return false;
        }
        return true;
    }
    //word界面 去修改
    @RequestMapping("toworkedit")
    public String toworkedit(Integer id,Model model){
        Job job = jobService.selectByPrimaryKey(id);
        model.addAttribute("job",job);
        return "empoption/workedit";
    }

    //word 修改
    @RequestMapping("workedit")
    @ResponseBody
    public Boolean workedit(Job job){
        jobService.updateByPrimaryKeySelective(job);
        return true;
    }

    //word界面 删除
    @RequestMapping("delwork")
    @ResponseBody
    public void delword(Integer id){
        jobService.deleteByPrimaryKey(id);
    }

    /**
     *
     * 教育背景
     *
     * */
    @RequestMapping("toeducation")
    public String toeducation(Integer id,HttpSession session){
        session.setAttribute("empid",id);
        return "empoption/educational";
    }

    //教育背景数据
    @RequestMapping("education")
    @ResponseBody
    public JsonData education(@Param("limit")int limit , @Param("page")int page, Integer id){
        pager.setPageSize(limit);
        pager.setCurrPage(page);
        jsonData.setCount(educationService.selCount(id));
        List<Education> list = (educationService.sellist(pager,id));
        jsonData.setData(list);
        return jsonData;
    }

    @RequestMapping("toeducationadd")
    public String toeducationadd(){
        return "empoption/addeducational";
    }
    //新增
    @RequestMapping("educationadd")
    @ResponseBody
    public Boolean educationadd(Education education,HttpSession session){
        Integer id = (Integer) session.getAttribute("empid");
        education.setEmpid(id);
        int bool = educationService.insertSelective(education);
        if (bool < 0){
            return false;
        }
        return true;
    }
    //去修改
    @RequestMapping("toeducationedit")
    public String toeducationedit(Integer id,Model model){
        Education education = educationService.selectByPrimaryKey(id);
        model.addAttribute("education",education);
        return "empoption/educationedit";
    }

    //修改
    @RequestMapping("educationedit")
    @ResponseBody
    public Boolean educationedit(Education education){
        educationService.updateByPrimaryKeySelective(education);
        return true;
    }
    //删除
    @RequestMapping("deleducation")
    @ResponseBody
    public void deleducation(Integer id){
        educationService.deleteByPrimaryKey(id);
    }


    /**
     *
     * 家庭联系数据
     *
     * */
    @RequestMapping("tolink")
    public String tolink(Integer id,HttpSession session){
        session.setAttribute("empid",id);
        return "empoption/linkman";
    }
    @RequestMapping("link")
    @ResponseBody
    public JsonData link(@Param("limit")int limit , @Param("page")int page, Integer id){
        pager.setPageSize(limit);
        pager.setCurrPage(page);
        jsonData.setCount(familyinfoService.selCount(id));
        List<Familyinfo> list = (familyinfoService.sellist(pager,id));
        jsonData.setData(list);
        return jsonData;
    }
    @RequestMapping("tolinkadd")
    public String tolinkadd(){
        return "empoption/linkmanadd";
    }
    //新增
    @RequestMapping("linkadd")
    @ResponseBody
    public Boolean linkadd(Familyinfo familyinfo,HttpSession session){
        Integer id = (Integer) session.getAttribute("empid");
        familyinfo.setEmpid(id);
        int bool = familyinfoService.insertSelective(familyinfo);
        if (bool < 0){
            return false;
        }
        return true;
    }
    //去修改
    @RequestMapping("tolinkedit")
    public String tolinkedit(Integer id,Model model){
        Familyinfo familyinfo = familyinfoService.selectByPrimaryKey(id);
        model.addAttribute("familyinfo",familyinfo);
        return "empoption/linkmanedit";
    }
    //修改
    @RequestMapping("linkedit")
    @ResponseBody
    public Boolean linkedit(Familyinfo familyinfo){
        familyinfoService.updateByPrimaryKeySelective(familyinfo);
        return true;
    }
    //删除
    @RequestMapping("dellink")
    @ResponseBody
    public void dellink(Integer id){
        familyinfoService.deleteByPrimaryKey(id);
    }

    /**
     * 考核指标
     *
     * */
    @RequestMapping("toassess")
    public String toassess(Integer id,HttpSession session){
        session.setAttribute("empid",id);
        return "empoption/assess";
    }
    @RequestMapping("assess")
    @ResponseBody
    public JsonData assess(@Param("limit")int limit , @Param("page")int page, Integer id){
        pager.setPageSize(limit);
        pager.setCurrPage(page);
        jsonData.setCount(aduitlogservice.selCount(id));
        List<Aduitlog> list = (aduitlogservice.sellist(pager,id));
        for(Aduitlog aduitlog : list){
            Aduitmodel aduitmodel = aduitmodelService.selectByPrimaryKey(aduitlog.getAduitmodelid());
            Empinfo emp = empinfoService.selectByPrimaryKey(aduitlog.getEmpid());
            if(aduitmodel != null){
                aduitlog.setAduitname(aduitmodel.getAduitname());
            }
            if(emp != null){
                aduitlog.setEmpname(emp.getEmp_name());
            }
        }
        jsonData.setData(list);
        return jsonData;
    }

    /**
     *
     * 证件上传
     *
     * */
    @RequestMapping("tovoucher")
    public String tovoucher(Integer id, Model model, HttpSession session){
        Empinfo empinfo = (Empinfo) session.getAttribute("user");//当前登录用户
        Empinfo empinfo1 = empinfoService.selectByPrimaryKey(id);//选择的用户
        model.addAttribute("empinfo",empinfo);
        model.addAttribute("empinfo1",empinfo1);
        return "empoption/voucher";
    }

    @RequestMapping("voucher")
    @ResponseBody
    public JsonData voucher(@Param("limit")int limit , @Param("page")int page, Integer id){
        pager.setPageSize(limit);
        pager.setCurrPage(page);
        jsonData.setCount(annexService.selCount(id));
        //找某个员工的附件
        List<Annex> list = (annexService.sellist(pager,"员工",id));
        for(Annex annex : list){
            Empinfo empinfo = empinfoService.selectByPrimaryKey(annex.getSeid());
            annex.setEmpname(empinfo.getEmp_name());
        }
        jsonData.setData(list);
        return jsonData;
    }

    @RequestMapping("voucheradd")
    public String voucheradd(MultipartFile file, Annex annex) throws IOException {
        Datadoc datadoc = Utils.fileUpload(file, Contants.CERT_FILE_SAVE_PATH);
        String newname = datadoc.getUrl();
        String date = datadoc.getOptime();
        annex.setSename("员工");
        annex.setAnnexnames("/"+newname);
        annex.setAnnexdate(date);
        annexService.insertSelective(annex);
        return "forward:tovoucher?id="+annex.getSeid();
    }

    //删除
    @RequestMapping("delvoucher")
    @ResponseBody
    public Boolean delvoucher(Integer id){
        annexService.deleteByPrimaryKey(id);
        return true;
    }

    //查看
    @RequestMapping("tolook")
    public String look(int id,Model model){
        Annex annex = annexService.selectByPrimaryKey(id);
        model.addAttribute("annex",annex);
        return "empoption/look";
    }

    //下载
    @RequestMapping("/download")
    @ResponseBody
    public void downloadFile(HttpServletRequest request,
                             HttpServletResponse response,
                             Integer id) throws UnsupportedEncodingException {


        Annex annex = annexService.selectByPrimaryKey(id);
        String fileName = annex.getAnnexnames();
//         如果文件名不为空，则进行下载
        if (fileName != null) {
            //设置文件路径
            String realPath = Contants.CERT_FILE_SAVE_PATH;
            File file = new File(realPath, fileName);

            // 如果文件名存在，则进行下载
            if (file.exists()) {
                // 配置文件下载
                response.setHeader("content-type", "application/octet-stream");
                response.setContentType("application/octet-stream");
                // 下载文件能正常显示中文
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

                // 实现文件下载
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    System.out.println("下载成功!");
                }
                catch (Exception e) {
                    System.out.println("下载失败！");
                }
                finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return;
    }
}
